const Pager = require('../../components/aotoo/core/index')
let lib = Pager.lib

function adapterList(data, keyword) {
  return data.map(item => {
    return {
      '@html': {
        content: item.title,
        listClass: 'xxss'
      },
      id: item.id,
      itemClass: 'item-rc',
      aim: item.aim || item.tap
    }
  })
}



function searchAdapter(params) {
  return {
    '@form': {
      data: [
        {
          input:[
            { title: {title: '', itemClass: 'icon-search'},  id: 'search', type: 'text', placeholder: params.placeholder, focus: params.focus, maxlength: '100', 'confirm-type': 'search', value: '', bindconfirm: 'onSubmitSearch', bindinput: 'bindinput' },
            params.btnStatus && { id: 'searchBtn', type: 'button', value: {title: params.btnText, aim: 'onCancel'} }
          ],
        },
      ],
      listClass: 'ss-search'
    },
    '@list': params.popList ? params.popList : {
      $$id: 'list',
      listClass: 'ss-search-pop'
    }
  }
}

module.exports = function mkDropdown(params) {
  let dft = {
    id: 'abc',
    data: [],
    adapter: adapterList,
    btnStatus: true,
    popList: null,      //pop结构从业务直接传入
  }

  let opts = Object.assign({}, dft, params)
  return {
    $$id: opts.$$id || opts.id,
    itemClass: 'search-contianer',
    ...searchAdapter(opts),
    methods: {
      __ready(){
        this.$list = this.activePage.getElementsById('list')
      },
      
      onCancel(e, params, inst) {
        typeof opts.btnCancelCb === 'function' ? opts.btnCancelCb.call() : wx.navigateBack()
      },
      bindinput(e, params, inst) {
        const _val = params.value
        if (!_val) {
          this.$list.reset()
        }
      },
      onSubmitSearch(e, params, inst) {
        const _searchVal = e.detail.value     //inst.getValue('search')
        if (_searchVal && typeof opts.btnCb === 'function') {
          this.$list = this.activePage.getElementsById('list')
          opts.btnCb.call(this, _searchVal, this.$list, opts.adapter)
        }
      }
    }
  }
}